//
//  CMDBSelectedView.swift
//  CMDB
//
//  Created by 王磊 on 18/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

extension CMDBSelectedView {
    
    override class func getInstance() -> CMDBSelectedView {
        
        return CMDBSelectedView(style: .Default, reuseIdentifier: "CellId")
    }
}

class CMDBSelectedView: UITableViewCell {
    
    private var buttonItem: UIButton = UIButton(type: .Custom)
    
    private var singleLine: UIView = UIView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        addOwnSubviews()
        
        configOwnSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension CMDBSelectedView {
    
    override func update(data: AnyObject?) {
        
        textLabel?.text = data as? String
    }
    
}

extension CMDBSelectedView {
    
    
    override func addOwnSubviews() {
        
        contentView.addSubview(singleLine)
        
        contentView.addSubview(buttonItem)
        
        contentView.sendSubviewToBack(buttonItem)
        
    }
    
    override func configOwnSubviews() {
        
        backgroundColor = UIColor.clearColor()
        
        selectionStyle = .None
        
        accessoryType = .DisclosureIndicator
        
        buttonItem.setBackgroundImage(UIImage.yy_imageWithColor(word12Color), forState: .Highlighted)
        
        buttonItem.setBackgroundImage(UIImage.yy_imageWithColor(UIColor.whiteColor()), forState: .Normal)
        
        textLabel?.font = CMDB_15_FONT
        
        textLabel?.textColor = word54Color
        
        singleLine.backgroundColor = word12Color
        
        buttonItem.addTarget(self, action: #selector(buttonItemClick), forControlEvents: .TouchUpInside)
    }
}
extension CMDBSelectedView {
    @objc private func buttonItemClick() {
        
        let argu = TSArguInfo()
        
        argu.view = self
        
        argu.itemType = .ButtonItem
        
        performSelWithArgu(argu)
    }
}
extension CMDBSelectedView {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        buttonItem.frame = bounds
        
        singleLine.frame = CGRectMake(0, height - 0.5, width, 0.5)
    }
}